/**
 * 应用全局状态管理
 */
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import type { Notification } from '@/types'

export const useAppStore = defineStore('app', () => {
  // State
  const sidebarCollapsed = ref(false)
  const theme = ref<'light' | 'dark'>('light')
  const notifications = ref<Notification[]>([])
  const loading = ref(false)
  const currentPageTitle = ref('')

  // Getters
  const unreadNotifications = computed(() =>
    notifications.value.filter(notification => !notification.is_read)
  )

  const unreadNotificationCount = computed(() =>
    unreadNotifications.value.length
  )

  // Actions
  const toggleSidebar = () => {
    sidebarCollapsed.value = !sidebarCollapsed.value
  }

  const setSidebarCollapsed = (collapsed: boolean) => {
    sidebarCollapsed.value = collapsed
  }

  const toggleTheme = () => {
    theme.value = theme.value === 'light' ? 'dark' : 'light'
    // 可以在这里添加主题切换的逻辑，比如修改CSS类名
    document.documentElement.setAttribute('data-theme', theme.value)
    localStorage.setItem('theme', theme.value)
  }

  const setTheme = (newTheme: 'light' | 'dark') => {
    theme.value = newTheme
    document.documentElement.setAttribute('data-theme', theme.value)
    localStorage.setItem('theme', theme.value)
  }

  const initializeTheme = () => {
    const savedTheme = localStorage.getItem('theme') as 'light' | 'dark' | null
    if (savedTheme) {
      setTheme(savedTheme)
    } else {
      // 检查系统主题偏好
      const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches
      setTheme(prefersDark ? 'dark' : 'light')
    }
  }

  const addNotification = (notification: Omit<Notification, 'id' | 'created_at'>) => {
    const newNotification: Notification = {
      ...notification,
      id: Date.now().toString(),
      created_at: new Date().toISOString()
    }

    notifications.value.unshift(newNotification)

    // 限制通知数量
    if (notifications.value.length > 100) {
      notifications.value = notifications.value.slice(0, 100)
    }
  }

  const removeNotification = (id: string) => {
    const index = notifications.value.findIndex(n => n.id === id)
    if (index > -1) {
      notifications.value.splice(index, 1)
    }
  }

  const markNotificationAsRead = (id: string) => {
    const notification = notifications.value.find(n => n.id === id)
    if (notification) {
      notification.is_read = true
    }
  }

  const markAllNotificationsAsRead = () => {
    notifications.value.forEach(notification => {
      notification.is_read = true
    })
  }

  const clearNotifications = () => {
    notifications.value = []
  }

  const setCurrentPageTitle = (title: string) => {
    currentPageTitle.value = title
  }

  const setLoading = (isLoading: boolean) => {
    loading.value = isLoading
  }

  // 快捷通知方法
  const showSuccess = (message: string, title = '成功') => {
    addNotification({
      title,
      message,
      type: 'success',
      is_read: false
    })
  }

  const showError = (message: string, title = '错误') => {
    addNotification({
      title,
      message,
      type: 'error',
      is_read: false
    })
  }

  const showWarning = (message: string, title = '警告') => {
    addNotification({
      title,
      message,
      type: 'warning',
      is_read: false
    })
  }

  const showInfo = (message: string, title = '信息') => {
    addNotification({
      title,
      message,
      type: 'info',
      is_read: false
    })
  }

  return {
    // State
    sidebarCollapsed,
    theme,
    notifications,
    loading,
    currentPageTitle,

    // Getters
    unreadNotifications,
    unreadNotificationCount,

    // Actions
    toggleSidebar,
    setSidebarCollapsed,
    toggleTheme,
    setTheme,
    initializeTheme,
    addNotification,
    removeNotification,
    markNotificationAsRead,
    markAllNotificationsAsRead,
    clearNotifications,
    setCurrentPageTitle,
    setLoading,
    showSuccess,
    showError,
    showWarning,
    showInfo
  }
})